﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using sexhealth.console.service.post;
using sexhealth.console.service.post.vo;
using System.Text;
using com.pro2e.web.util.page;
using com.pro2e.web.config;
public partial class admin_UC01_0109htmlupload : System.Web.UI.Page
{
    ConfigHelper configHelper = new ConfigHelper();
    PostService postService = new PostService();
    //html上傳
    int channelId = 23;
    private int PostMode
    {
        get { if (ViewState["postmode"] == null) { ViewState["postmode"] = 0; } return int.Parse(ViewState["postmode"].ToString()); }
        set { ViewState["postmode"] = value; }
    }
    private string FileName
    {
        get { if (ViewState["filename"] == null) { ViewState["filename"] = string.Empty; } return ViewState["filename"].ToString(); }
        set { ViewState["filename"] = value; }
    }
   
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            showListMode();
            GetList();

        }
    }
    protected void ddlChannel_SelectedIndexChanged(object sender, EventArgs e)
    {
        GetList();
    }
    protected void btnAddNew_Click(object sender, EventArgs e)
    {
        PostMode = 0;
        showModifyMode();
    }
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        showListMode();
    }
    protected void btnSave_Click(object sender, EventArgs e)
    {
        HttpFileCollection hfc = Request.Files;
        if (hfc.Count > 0)
        {
            hfc[0].SaveAs(Server.MapPath("~\\") + "\\upload\\html\\" + System.IO.Path.GetFileName(hfc[0].FileName));
            FileName = System.IO.Path.GetFileName(hfc[0].FileName);
        }
        if (PostMode == 0)
        {
    
            PostVO post = new PostVO();
            post.Title = txtTitle.Text.Trim();
            post.KeyWord = txtKeyWord.Text;
            post.BelongToChannelId = channelId;
            post.FileLink = FileName;
            postService.InsertPost(post);

        }
        else
        {

            PostVO post = postService.DaoGetVOById<PostVO>(PostMode);
            post.Title = txtTitle.Text.Trim();
            post.KeyWord = txtKeyWord.Text;
            post.BelongToChannelId = channelId;
            if (hfc.Count > 0)
            {
                post.FileLink = FileName;
                txtLink.Text = "http://" + configHelper.Host + "/upload/html/" + post.FileLink;
            }
            postService.UpdatePost(post);
        }
        showListMode();
        GetList();

    }
    private void showListMode()
    {
        pnlGv.Visible = true;
        pnlContent.Visible = false;
        btnAddNew.Visible = true;
    }
    private void showModifyMode()
    {
        pnlGv.Visible = false;
        pnlContent.Visible = true;
        btnAddNew.Visible = false;
    }
    private void GetList()
    {
    
            gvResult.DataSource = postService.GetPostList(channelId);
            gvResult.DataBind();
     
    }
    protected void gvResult_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        
        string cmdName = e.CommandName;
        int PostNo = int.Parse(e.CommandArgument.ToString());
        PostVO post = postService.DaoGetVOById<PostVO>(PostNo);
        switch (cmdName)
        {
            case "myModify":

                txtTitle.Text = post.Title;
                txtKeyWord.Text = post.KeyWord;
                if (post.FileLink != string.Empty) 
                {
                    txtLink.Text = "http://" + configHelper.Host + "/upload/html/" + post.FileLink;
                }
                showModifyMode();
                ShowContent();
                PostMode = PostNo;
                break;

            case "myDel":
                postService.DaoDelete(post);
                GetList();
                break;

            default:
                break;
        }
    }
    private void ShowContent()
    {
        IList<PostVO> postList = postService.GetPostList(channelId);
        if (postList.Count > 0)
        {
            txtTitle.Text = postList[0].Title;
            txtKeyWord.Text = postList[0].KeyWord;
            
        }

    }

}
